package com.sgb.kjwl.view.ui.activity.self.lease;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.PopupWindow;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityLeaseMemberListBinding;
import com.sgb.kjwl.databinding.ActivitySelfLeaseListBinding;
import com.sgb.kjwl.di.machine.DaggerMachineComponent;
import com.sgb.kjwl.di.machine.MachineModule;
import com.sgb.kjwl.model.entity.self.LeaseMemberListEntity;
import com.sgb.kjwl.view.ui.activity.self.publish.LeaseMachineActivity;
import com.sgb.kjwl.view.ui.adapter.self.publish.LeaseMemberStateAdapter;
import com.sgb.kjwl.viewmodel.self.publish.LeaseMemberListModel;
import com.sgb.link.event.LeaseEvent;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.CommonDialog;
import com.swgk.core.event.EventConstant;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.view.selection.CommonPopEntity;
import com.swgk.core.view.selection.MemberStatusView;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

/**
 * 会员价管理
 */
public class LeaseMemberListActivity extends AppActivity {
    private ActivityLeaseMemberListBinding binding;

    @Inject
    LeaseMemberListModel viewModel;
    private LeaseMemberStateAdapter adapter;
    private List<CommonPopEntity> classDatas;
    private List<CommonPopEntity> statusDatas;
    private String statusNo = "";//状态no
    private String classNo = "";//分类no
    private int vipDataCount = 0;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerMachineComponent.builder().appComponent(appComponent)
                .machineModule(new MachineModule(this))
                .build()
                .inject(this);
    }


    public static void start(Context context) {
        start(context, null);
    }

    public static void start(Context context, Intent extras) {
        Intent intent = new Intent();
        intent.setClass(context, LeaseMemberListActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        if (extras != null) {
            intent.putExtras(extras);
        }
        context.startActivity(intent);
    }


    @Override
    protected void initView() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        binding = DataBindingUtil.setContentView(LeaseMemberListActivity.this, R.layout.activity_lease_member_list);
        initXRecyclerView();
        initPopWindow();
        binding.topLayout.ivBack.setOnClickListener(v -> finish());
        binding.topLayout.titleView.setText("会员价管理");
        binding.topLayout.rightView.setText("新增");
        binding.topLayout.rightView.setOnClickListener(v -> startPrice());
        binding.msvClass.setOnClickListener(view -> binding.msvClass.showPop());
        binding.msvStatus.setOnClickListener(view -> binding.msvStatus.showPop());
    }

    @Override
    protected void init() {
        initClassData();
        initStatusData();
        getData(false);
        viewModel.getPublishCount();
    }


    private void initXRecyclerView() {
        if (adapter == null) {
            adapter = new LeaseMemberStateAdapter(getActivity(), null);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.rlvView.setLayoutManager(layoutManager);
        binding.rlvView.addItemDecoration(new RecyclerCustomDecoration(0,
                0,
                0,
                getResources().getDimension(R.dimen.dp1), getResources().getColor(R.color.color_F5F5F5)));
        binding.rlvView.setAdapter(adapter);
        binding.rlvView.setLoadingMoreEnabled(true);
        binding.rlvView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getPublishListModel(false);
            }

            @Override
            public void onLoadMore() {
                viewModel.getPublishListModel(true);
            }
        });
        adapter.setItemCallBack(new LeaseMemberStateAdapter.ItemCallBack() {

            @Override
            public void onStop(String id, String type) {
                optState(id);
            }

            @Override
            public void onDetail(String id, String type) {
                viewModel.getH5Url(id);
            }
        });
    }

    private void initPopWindow() {
        binding.msvClass.initPopup(binding.grayLayout, binding.popLine, new MemberStatusView.OnChooseListener() {
            @Override
            public void onLoadChooseData(String mKeyName) {

            }

            @Override
            public void onChooseResult(String mKeyName, CommonPopEntity entity, Object... objects) {
                classNo = entity.getNo();
                getData(false);
            }
        });

        binding.msvStatus.initPopup(binding.grayLayout, binding.popLine, new MemberStatusView.OnChooseListener() {
            @Override
            public void onLoadChooseData(String mKeyName) {

            }

            @Override
            public void onChooseResult(String mKeyName, CommonPopEntity entity, Object... objects) {
                statusNo = entity.getNo();
                getData(false);
            }
        });
    }

    private void getData(boolean isLoadMore) {
        viewModel.map.get().put("type", classNo);
        viewModel.map.get().put("vipPriceStatus", statusNo);
        viewModel.getPublishListModel(isLoadMore);
    }

    public void showList(List<LeaseMemberListEntity> datas, boolean isLoadMore) {
        if (isLoadMore) {
            binding.rlvView.loadMoreComplete();
            if (datas.size() != 0) {
                adapter.addDatas(datas);
            }
        } else {
            if (datas == null || datas.size() == 0) {
                datas = new ArrayList<>();
                binding.emptyView.setVisibility(View.VISIBLE);
            } else {
                binding.emptyView.setVisibility(View.GONE);
            }
            binding.rlvView.refreshComplete();
            binding.rlvView.setLoadingMoreEnabled(true);
            adapter.setDatas(datas);
        }
    }

    public void showCount(int count) {
      this.vipDataCount = count;
    }

    private void optState(String id) {
        new CommonDialog(getActivity()).builder().setPositiveButton("确定").setNegativeButton("取消")
                .setTitle("您确定结束会员价吗？")
                .setMsg("结束会员价后恢复市场价")
                .setDialogCallBack(new CommonDialog.DialogCallBack() {
                    @Override
                    public void onRightClick() {
                        viewModel.leaseVipCancel(id);
                    }

                    @Override
                    public void onLeftClick() {

                    }
                }).show();

    }

    private void onPublishDialog() {
        new CommonDialog(getActivity()).builder().setPositiveButton("立即发布").setNegativeButton("取消")
                .setTitle("暂无数据")
                .setMsg("请在租赁发布中发布租赁数据")
                .setDialogCallBack(new CommonDialog.DialogCallBack() {
                    @Override
                    public void onRightClick() {
                        LeaseMachineActivity.start(getActivity());
                    }

                    @Override
                    public void onLeftClick() {

                    }
                }).show();
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(LeaseEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_LEASE_MEMBER_LIST_REFRESH) {
            viewModel.getPublishListModel(false);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void initClassData() {
        //状态:1 机械 2 设备3 车辆4 房产 5 土地 6工厂 7 农机 空字符 全部
        classDatas = new ArrayList<>();
        classDatas.add(new CommonPopEntity("", "不限"));
        classDatas.add(new CommonPopEntity("1", "机械租赁"));
//        classDatas.add(new DictEntity3("2", "设备租赁"));
//        classDatas.add(new DictEntity3("3", "车辆租赁"));
//        classDatas.add(new DictEntity3("4", "房产租赁"));
//        classDatas.add(new DictEntity3("5", "土地租赁"));
//        classDatas.add(new DictEntity3("6", "工厂租赁"));
//        classDatas.add(new DictEntity3("7", "农机租赁"));
        binding.msvClass.setData(classDatas, classNo);
    }

    private void initStatusData() {
        //状态:0草稿，1 上架 2 下架 3 已拒绝，4 审核中
        statusDatas = new ArrayList<>();
        statusDatas.add(new CommonPopEntity("", "不限"));
        statusDatas.add(new CommonPopEntity("1", "生效中"));
        statusDatas.add(new CommonPopEntity("2", "已失效"));
        binding.msvStatus.setData(statusDatas, statusNo);
    }

    //新增会员价格
    private void startPrice(){
        if(vipDataCount==0){
            onPublishDialog();
        }else {
            LeaseMemberPriceListActivity.start(LeaseMemberListActivity.this);
        }
    }

}

